/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.excel.handler;

import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;

/**
 * 单元格样式<br>
 * <hr />
 *
 * <table border="1">
 *   <thead>
 *     <tr><th>方法</th><th>注释</th></tr>
 *   </thead>
 *   <tbody>
 *     <tr><td>beforeCellCreate</td><td>在创建单元格之前调用</td></tr>
 *     <tr><td>afterCellCreate</td><td>在创建单元格后调用</td></tr>
 *     <tr><td>afterCellDataConverted</td><td>在转换单元格数据后调用</td></tr>
 *     <tr><td>afterCellDispose</td><td>在单元格上的所有操作完成后调用</td></tr>
 * </table>
 *
 * @author 冰羽
 * @version 2.0.0
 */
public class CellStyleHandler extends HorizontalCellStyleStrategy implements OrderHandler {

  public CellStyleHandler() {
    this(getDefaultHeadWriteCellStyle(), getDefaultContentWriteCellStyle());
  }

  public CellStyleHandler(WriteCellStyle headWriteCellStyle, WriteCellStyle contentWriteCellStyle) {
    super(headWriteCellStyle, contentWriteCellStyle);
  }

  public static WriteCellStyle getDefaultHeadWriteCellStyle() {
    // 头样式
    WriteCellStyle style = new WriteCellStyle();
    style.setWrapped(true);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setLocked(true);
    style.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setBorderTop(BorderStyle.THIN);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBorderLeft(BorderStyle.THIN);
    style.setBorderRight(BorderStyle.THIN);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setHorizontalAlignment(HorizontalAlignment.LEFT);

    WriteFont font = new WriteFont();
    font.setFontName("宋体");
    font.setFontHeightInPoints((short) 11);
    font.setBold(true);

    style.setWriteFont(font);

    return style;
  }

  public static WriteCellStyle getDefaultContentWriteCellStyle() {
    // 内容样式
    WriteCellStyle style = new WriteCellStyle();
    style.setHorizontalAlignment(HorizontalAlignment.LEFT);

    /**
     * 内容单元格线框暂时使用实线，需要支持配置
     *
     * <pre>
     *   style.setBorderLeft(BorderStyle.DASHED);
     *   style.setBorderTop(BorderStyle.DASHED);
     *   style.setBorderRight(BorderStyle.DASHED);
     *   style.setBorderBottom(BorderStyle.DASHED);
     * </pre>
     */
    style.setBorderLeft(BorderStyle.THIN);
    style.setBorderTop(BorderStyle.THIN);
    style.setBorderRight(BorderStyle.THIN);
    style.setBorderBottom(BorderStyle.THIN);

    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setHorizontalAlignment(HorizontalAlignment.LEFT);

    WriteFont font = new WriteFont();
    font.setFontName("宋体");
    font.setFontHeightInPoints((short) 11);
    font.setBold(false);

    style.setWriteFont(font);

    return style;
  }

  @Override
  public int order() {
    return this.wrapper(OrderStrategyType.CELL_STYLE);
  }
}
